package tgc.edu.ljc.web.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.ljc.custom.AjaxResult;
import tgc.edu.ljc.custom.CommonController;
import tgc.edu.ljc.custom.TablePageable;
import tgc.edu.ljc.entity.SysFile;
import tgc.edu.ljc.service.SysFileService;
import tgc.edu.ljc.web.form.SysFileForm;

@Controller
@RequestMapping(value="/recycleFile") 
public class RecycleFileController{
	@Autowired
	private SysFileService sysfileservice;
	
	@RequestMapping(value="/manage")
	public void manage() {
	} 
	@RequestMapping(value="/page")
	@ResponseBody
	public Object page(TablePageable pageParam,SysFileForm form) {
		PageRequest pageable = pageParam.bulidPageRequest();
		Specification<SysFile> spec = buildSpec(form);
		Page<SysFile> page=sysfileservice.findAll(spec, pageable);
		return AjaxResult.bulidPageResult(page);
	}
	private Specification<SysFile> buildSpec(SysFileForm form) {
		Specification<SysFile> spec = new Specification<SysFile>() {
		/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

		@Override
		public Predicate toPredicate(Root<SysFile> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules=new HashSet<>();
				if(StringUtils.hasText(form.getFileName())) {
					Predicate name = cb.like(root.get("fileName"), "%"+form.getFileName()+"%");
					rules.add(name);
				}
				if(StringUtils.hasText(form.getFileUser())) {
					Predicate difficultylevel = cb.like(root.get("fileUser"), "%"+form.getFileUser()+"%");
					rules.add(difficultylevel);
				}
				UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
				String user = userDetails.getUsername();
				if(StringUtils.hasText(user)) {
					System.out.println(user);
					Predicate uploadsUsername = cb.equal(root.get("uploadsUsername"), user);
					rules.add(uploadsUsername);
				}
				rules.add(cb.equal(root.get("isUsed"), false));
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}
	@RequestMapping(value="/delete")
	@ResponseBody
	public Object delete(Integer id) {
		try {
			sysfileservice.deleteFile(id);
			return new AjaxResult("删除成功");
		} catch (Exception e) {
			return new AjaxResult(false, "删除失败");
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/restore")
	@ResponseBody
	public Object restore(@RequestBody Object map) {
		try {
			Map<String,List<Integer>> hashMap = new HashMap<String,List<Integer>>();
			hashMap = (Map<String, List<Integer>>) map;
			List<Integer> result = hashMap.get("id");
			for (Integer integer : result) {
				SysFile findById = sysfileservice.findById(integer);
				findById.setIsUsed(true);
				sysfileservice.save(findById);
			}
			return new AjaxResult("文件已还原，还原"+result.size()+"个文件！");
		} catch (Exception e) {
			return new AjaxResult(false, "还原失败");
		}
	}
}
